//
//  DriverAddVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/6.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "DriverAddVC.h"
#import "DriverAddCell.h"
#import "DriverAddInPutCell.h"
#import "DriverChooseCell.h"
#import "StatusChangeCell.h"
#import "DriverAddModel.h"
#import "DriverAddInfoCell.h"
#import "DriverAddNoDataCell.h"
#import "DriverAddInviteVC.h"

@interface DriverAddVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) DriverAddModel *driverModel;

@end

@implementation DriverAddVC

-(DriverAddModel *)driverModel {
    if (!_driverModel) {
        _driverModel = [DriverAddModel new];
        _driverModel.hasSearch = NO;
    }
    return _driverModel;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
//        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 30 - 92 - 15 - 30, 44)];
//        [nextBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
//        nextBtn.layer.masksToBounds = YES;
//        nextBtn.layer.cornerRadius = 6;
//        [nextBtn setTitle:@"保存并继续添加司机" forState:0];
//        [nextBtn setTitleColor:HexColor(0x3D68EB) forState:0];
//        nextBtn.titleLabel.font = BoldFontOfSize(16);
//        [nextBtn addTarget:self action:@selector(nextClick) forControlEvents:UIControlEventTouchUpInside];
//        [_bottomView addSubview:nextBtn];
        
        UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [saveBtn setBackgroundColor:Color_ThemeColor];
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.cornerRadius = 6;
        [saveBtn setTitle:@"保存" forState:0];
        [saveBtn setTitleColor:HexColor(0xffffff) forState:0];
        saveBtn.titleLabel.font = BoldFontOfSize(16);
        [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:saveBtn];
    }
    return _bottomView;
}

//- (void)nextClick {
//    [self requestForSav:NO];
//}

- (void)saveClick {
    [self requestForSav:YES];
}

- (void)requestForSav:(BOOL)isPop {
    if (!self.driverModel.sw_id.length) {
        [MBProgressHUD leShowFailedHUD:self.view message:@"请选择司机"];
        return;
    }
    [HttpRequest PostWithApi:LogisticsSupplierAddDriver_V_1_0 params:@{@"s_id":self.driverModel.s_id,@"sw_id":self.driverModel.sw_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            __weak_self;
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功" delayOperation:^{
                if (isPop) {
                    [weakself.rt_navigationController popViewControllerAnimated:YES];
                }else {
                    weakself.driverModel = [DriverAddModel new];
                    [weakself.tableView reloadData];
                }
            }];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加司机";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.driverModel.hasSearch ? ([self.driverModel.sw_id intValue] ? 8 : 1) : 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DriverChooseCell *cell = [DriverChooseCell DriverChooseCellWithTableView:tableView];
        __weak_self;
        cell.searchBlock = ^(NSString * _Nonnull phone) {
            [weakself requestForDriverWithPhone:phone];
        };
        return cell;
    }else {
        if ([self.driverModel.sw_id intValue]) {
            DriverAddInfoCell *cell = [DriverAddInfoCell DriverAddInfoCellWithTableView:tableView];
            if (indexPath.row == 0) {
                cell.titleStr = @"司机姓名";
                cell.contentStr = self.driverModel.username.length ? self.driverModel.username : @"";
            }else if (indexPath.row == 1) {
                cell.titleStr = @"身份证号";
                cell.contentStr = self.driverModel.id_card.length ? self.driverModel.id_card : @"";
            }else if (indexPath.row == 2) {
                cell.titleStr = @"驾驶证号";
                cell.contentStr = self.driverModel.license_num.length ? self.driverModel.license_num : @"";
            }else if (indexPath.row == 3) {
                cell.titleStr = @"驾驶证类型";
                cell.contentStr = self.driverModel.type.length ? self.driverModel.type : @"";
            }else if (indexPath.row == 4) {
                cell.titleStr = @"联系电话";
                cell.contentStr = self.driverModel.phone.length ? self.driverModel.phone : @"";
            }else if (indexPath.row == 5) {
                cell.titleStr = @"邮箱";
                cell.contentStr = self.driverModel.email.length ? self.driverModel.email : @"";
            }else if (indexPath.row == 6) {
                cell.titleStr = @"紧急联系人";
                cell.contentStr = self.driverModel.tel_name.length ? self.driverModel.tel_name : @"";
            }else if (indexPath.row == 7) {
                cell.titleStr = @"紧急联系人电话";
                cell.contentStr = self.driverModel.tel_num.length ? self.driverModel.tel_num : @"";
            }
            return cell;
        }else {
            DriverAddNoDataCell *cell = [DriverAddNoDataCell DriverAddNoDataCellWithTableView:tableView];
            __weak_self;
            cell.inviteBlock = ^{
                DriverAddInviteVC *vc = [DriverAddInviteVC new];
                [weakself.rt_navigationController pushViewController:vc animated:YES];
            };
            return cell;
        }
    }
}

- (void)requestForDriverWithPhone:(NSString *)phone {
    [HttpRequest PostWithApi:LogisticsSupplierDriverSearch_V_1_0 params:@{@"phone":phone} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.driverModel = [DriverAddModel mj_objectWithKeyValues:responseObject];
        }
        self.driverModel.hasSearch = YES;
        [self.tableView reloadData];
    } fail:^(NSError * _Nonnull error) {

    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DriverChooseCell *cell = [DriverChooseCell DriverChooseCellWithTableView:tableView];
        return cell.height;
    }else {
        if ([self.driverModel.sw_id intValue]) {
            DriverAddInfoCell *cell = [DriverAddInfoCell DriverAddInfoCellWithTableView:tableView];
            return cell.height;
        }else {
            DriverAddNoDataCell *cell = [DriverAddNoDataCell DriverAddNoDataCellWithTableView:tableView];
            return cell.height;
        }
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

@end
